package com.matt.controller.pad;


import com.google.common.collect.ImmutableMap;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.mp.ParentUser;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.utils.IpHelper;
import com.matt.service.AppUserService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/6/9
 * *******************************
 */
@Controller
@RequestMapping("/pad/un_login")
@Slf4j
@Api(tags = "系统登录 - ")
public class PadLoginController {

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private AppUserService appUserService;


    @RequestMapping(value = "/send_sms", method = RequestMethod.GET)
    @ResponseBody
    public StatusResponse sendSms(String mobile) {
        Map result = null;
        ParentUser users =  appUserService.selectByPhone(mobile);//查询家长身份
        System.out.println("users:"+users);

        boolean flag =  ssoCallService.send_sms(mobile);
        System.out.println("flag:"+flag);
        if(flag){
            result =  ImmutableMap.of("msg", "短信发送成功");
        }else{
            throw new BusinessException(ErrorCode.ERROR, "短信发送错误");
        }

        System.out.println("result:"+result);
        return StatusResponse.success(result);
    }
//
//    @RequestMapping(value = "/validSmsCode")
//    @ResponseBody
//    public StatusResponse validCode(String phone, String code) {
//        boolean flag = ssoCallService.validSmsCode(phone,code);
//        if(!flag){
//            throw new BusinessException(ErrorCode.ERROR, "短信验证码不匹配");
//        }
//        return StatusResponse.success(flag);
//    }


    @PostMapping(value = "/phone_pass_login")
    @ResponseBody
    public StatusResponse phonePassLogin(String mobile, String password, @RequestHeader(required = false) String platformId, HttpServletRequest request){
        String ticket = ssoCallService.passLogin(mobile, password, IpHelper.getRequestIpAddr(request), 1,platformId);
        return StatusResponse.success(ticket);
    }

}
